package com.jt.www.admin.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.jt.www.admin.user.service.UserService;
import com.jt.www.exception.BizException;
import com.jt.www.model.entity.User;
import com.jt.www.model.remote.user.EduAuthenInfoDetails;
import com.jt.www.model.remote.user.LoginResultVO;
import com.jt.www.model.remote.user.RoleResourceVO;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.vo.woxuebao.OrgElementVO;
import com.jt.www.remote.UserClient;
import com.jt.www.util.CommonUtils;
import com.jt.www.util.ConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    public static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    ConfigUtils configUtils;
    @Resource
    private UserClient userClient;

    @Override
    public User findUserByCode(String userCode) {
        ResponseEntity<GenericDataResponse<LoginResultVO>> result = userClient.queryInfoByUserCode(userCode, configUtils.getSysChannelUser());
        if (result == null) {
            throw new BizException("用户信息调用失败");
        }
        LoginResultVO loginResultVO = result.getBody().getData();
        if (loginResultVO == null) {
            throw new BizException("找不到该用户");
        }
        return convertUser(loginResultVO);
    }


    @Override
    public User findEduUserByCode(String userCode) {
        ResponseEntity<GenericDataResponse<EduAuthenInfoDetails>> result = userClient.queryAuthentEduInfo(userCode);
        if (result == null) {
            throw new BizException("用户信息调用失败");
        }
        EduAuthenInfoDetails eduAuthenInfoDetails = result.getBody().getData();
        if (eduAuthenInfoDetails == null) {
            throw new BizException("找不到该用户");
        }
        User user = new User();
        user.setCode(eduAuthenInfoDetails.getUserCode());
        user.setName(eduAuthenInfoDetails.getOrgFullName());
        List<OrgElementVO> custOrganizationElementList = eduAuthenInfoDetails.getCustOrganizationElementList();
        for (OrgElementVO orgElementVO : custOrganizationElementList) {
            if (orgElementVO.getElementCode().equals("linkManEmail")) {
                String email = orgElementVO.getElementValues();
                user.setEmail(email);
            }
        }
        return user;
    }

    @Override
    public String getBranchCodes(String findCode, String userCode) {
        // 权限设置
        try {
            ResponseEntity<GenericDataResponse<Map<String, Object>>> responseEntity = userClient.queryUserRoleAndAuthority(userCode, "sys_edu_man");
            LinkedHashMap<String, String> map = (LinkedHashMap) responseEntity.getBody().getData().get("dataPopedom");
            log.info("{} 用户权限查询出参:{}", findCode, JSON.toJSONString(responseEntity));
            String jtBranchs = map.get("jtBranchs");

            return jtBranchs;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("{} 用户权限查询异常!", findCode);
            return "1";
        }
    }

    private User convertUser(LoginResultVO loginResultVO) {
        User user = new User();
        user.setCode(loginResultVO.getUserCode());
        user.setName(loginResultVO.getUserName());
        user.setOrgCode(loginResultVO.getBranchCode());

        user.setEmail(loginResultVO.getEmail());

        ArrayList<String> roleTyps = new ArrayList();
        if (CommonUtils.isEmpty(loginResultVO.getRoleResourceVOS())) {
            throw new BizException("用户权限为空");
        }
        for (RoleResourceVO roleResourceVO : loginResultVO.getRoleResourceVOS()) {
            roleTyps.add(roleResourceVO.getRoleCode());
        }
        user.setRoleTypes(roleTyps);
        if (loginResultVO.getProvinceCode() != null && loginResultVO.getProvinceCode() != "") {
            user.setProvinceCode(loginResultVO.getProvinceCode());
        }
        if (loginResultVO.getCityCode() != null && loginResultVO.getCityCode() != "") {
            user.setCityCode(loginResultVO.getCityCode());
        }
        return user;
    }

}
